package com.example.tabbar.fragment


import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.tabbar.R
import com.example.tabbar.adapter.NavigationAdapter
import com.example.tabbar.room.bean.NavItem
import com.example.tabbar.databinding.FragmentMaintenanceBinding


class MaintenanceFragment : Fragment() {
    private var binding: FragmentMaintenanceBinding? = null
    private var temperatureFragment: TemperatureFragment? = null
    private var recyclerView: RecyclerView? = null
    private var adapter: NavigationAdapter? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {

        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentMaintenanceBinding.inflate(inflater, container, false)
        return binding!!.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        if (savedInstanceState == null) {
            temperatureFragment = TemperatureFragment.newInstance()
            loadFragment(temperatureFragment!!)
        }

        initRecyclerView()
    }

    private fun initRecyclerView() {
        recyclerView = binding?.maintainRecyclerView
        adapter = NavigationAdapter()
        recyclerView?.adapter = adapter
        recyclerView?.layoutManager = LinearLayoutManager(requireContext())

        val navItems = navItems()

        adapter?.setItems(navItems)
        adapter?.setOnItemClickListener { position ->
            when (position) {
                0 -> {
                    loadFragment(TemperatureFragment.newInstance())
                }
                1 -> {
                    loadFragment(PressureFragment.newInstance())
                }
                2 -> {
                    loadFragment(ConcentrationFragment.newInstance())
                }
                3 -> {
                    loadFragment(BatteryFragment.newInstance())
                }
                4 -> {
                    loadFragment(TrafficFragment.newInstance())
                }
                5 -> {
                    loadFragment(SystemFragment.newInstance())
                }
                6 -> {
                    loadFragment(CalibrationFragment.newInstance())
                }
            }
        }

    }

    private fun navItems(): List<NavItem> {
        val navItems = listOf(
            NavItem(R.drawable.temp_humidity, "温湿度校准"),
            NavItem(R.drawable.pressure, "压力校准"),
            NavItem(R.drawable.concentration, "浓度校准"),
            NavItem(R.drawable.battery, "电池校准"),
            NavItem(R.drawable.traffic, "流量校准"),
            NavItem(R.drawable.settings, "系统配置"),
            NavItem(R.drawable.expiration, "校准到期")
        )
        return navItems
    }

    private fun loadFragment(fragment: Fragment) {

        // 使用hide/show代替replace提高性能
        val transaction = childFragmentManager.beginTransaction()

        // 隐藏所有Fragment
        childFragmentManager.fragments.forEach {
            transaction.hide(it)
        }

        // 显示目标Fragment或添加新Fragment
        if (fragment.isAdded) {
            transaction.show(fragment)
        } else {
            transaction.add(R.id.maintenance_content, fragment)
        }

        transaction.commit()
    }



    companion object {

        @JvmStatic
        fun newInstance() =
            MaintenanceFragment().apply {
                arguments = Bundle().apply {

                }
            }
    }
}